local hx__sihai = fk.CreateSkill {
  name = "hx__sihai",
  tags = { Skill.Limited },
}

hx__sihai:addEffect(fk.GameStart, {
  anim_type = "control",
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__sihai.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__sihai.name)
    player.room:doAnimate("InvokeSkill", {
      name = "hx__sihai",
      player = player.id,
      skill_type = "control",
    })
    room:addPlayerMark(player, "@@cangying")
    room:addPlayerMark(player, "@@wenzi")
    room:addPlayerMark(player, "@@laoshu")
    room:addPlayerMark(player, "@@zhanglang")
  end,
})

hx__sihai:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__sihai.name) and player.phase == Player.Start and
      (player:getMark("@@cangying") > 0 or player:getMark("@@wenzi") > 0 or
       player:getMark("@@laoshu") > 0 or player:getMark("@@zhanglang") > 0)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player,{
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "消耗所有“四害”标记选择一名其他角色令其依次执行“四害”效果！",
      skill_name = hx__sihai.name,
    })
    if #to == 0 then return end
    local tos = to[1]
    room:removePlayerMark(player, "@@cangying")
    room:removePlayerMark(player, "@@wenzi")
    room:removePlayerMark(player, "@@laoshu")
    room:removePlayerMark(player, "@@zhanglang")
    room:addPlayerMark(tos, "@@hx__sihai_target")
    local skills2 = table.map(table.filter(tos.player_skills, function(skill)
      return skill:isPlayerSkill(tos)
    end), function(s)
      return s.name
    end)
    room:setTag("cangying2", skills2)
    room:addPlayerMark(tos, "@@cangying-tos")
    room:handleAddLoseSkills(tos, "-"..table.concat(skills2, "|-"))
  end,
})

hx__sihai:addEffect(fk.RoundStart, {
  anim_type = "control",
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local tos = table.filter(player.room:getAlivePlayers(), function(p)
      return p:getMark("@@hx__sihai_target") > 0
    end)
    return player:hasSkill(hx__sihai.name) and #tos > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__sihai.name)
    player.room:doAnimate("InvokeSkill", {
      name = "hx__sihai",
      player = player.id,
      skill_type = "control",
    })
    local tos = table.filter(player.room:getAlivePlayers(), function(p)
      return p:getMark("@@hx__sihai_target") > 0
    end)
    --无苍蝇因此处理蚊子
    if tos[1]:getMark("@@cangying-tos") == 0 and tos[1]:getMark("hx__sihai_can1") > 0 then
      room:addPlayerMark(tos[1], "@@wenzi-tos")
      tos[1]:turnOver()
      room:setPlayerMark(tos[1], "hx__sihai_can1", 0)
    end
    if tos[1]:getMark("@@laoshu-tos") == 0 and tos[1]:getMark("@@cangying-tos") == 0 and tos[1]:getMark("hx__sihai_can2") > 0 then
      local subtype = {Card.SubtypeWeapon, Card.SubtypeArmor, Card.SubtypeOffensiveRide, Card.SubtypeDefensiveRide, Card.SubtypeTreasure}
      local slots = {}
      for _, type in ipairs(subtype) do
        for i = 1, #player:getAvailableEquipSlots(type), 1 do
          table.insert(slots, Util.convertSubtypeAndEquipSlot(type))
        end
      end
      room:addPlayerMark(tos[1], "@@laoshu-tos")
      room:abortPlayerArea(tos[1], slots)
      room:setPlayerMark(tos[1], "hx__sihai_can2", 0)
    end
    --处理蟑螂
    if tos[1]:getMark("@@laoshu-tos") == 0 and tos[1]:getMark("@@cangying-tos") == 0 and tos[1]:getMark("@@wenzi-tos") == 0 and tos[1]:getMark("hx__sihai_can3") > 0 then
      room:addPlayerMark(tos[1], "@@zhanglang-tos")
      room:setPlayerMark(tos[1], "hx__sihai_can3", 0)
    end
  end,
})

hx__sihai:addEffect(fk.RoundEnd, {
  mute = true,
  global = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local tos = table.filter(player.room:getAlivePlayers(), function(p)
      return p:getMark("@@hx__sihai_target") > 0
    end)
    return player:hasSkill(hx__sihai.name) and #tos > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    --恢复技能
    local tos = table.filter(room:getAlivePlayers(), function(p)
      return p:getMark("@@cangying-tos") > 0
    end)
    if #tos > 0 then
      local skills2 = {room:getTag("cangying2")} or {}
      for _, skill_name in ipairs(skills2) do
        room:handleAddLoseSkills(tos[1], skill_name, nil, false, true)
      end
      room:removePlayerMark(tos[1], "@@cangying-tos")
      room:addPlayerMark(tos[1], "hx__sihai_can1",1)
    end
    --翻正面
    local tos2 = table.filter(room:getAlivePlayers(), function(p) 
      return p:getMark("@@wenzi-tos") > 0
    end)
    if #tos2 > 0 then
      room:removePlayerMark(tos2[1], "@@wenzi-tos")
      if not tos2[1].faceup then
        tos2[1]:turnOver()
      end
      room:addPlayerMark(tos2[1], "hx__sihai_can2",1)
    end
    --恢复装备栏
    local tos3 = table.filter(room:getAlivePlayers(), function(p) 
      return p:getMark("@@laoshu-tos") > 0
    end)
    if #tos3 > 0 then
      local slots2 = table.simpleClone(tos3[1].sealedSlots)
      room:resumePlayerArea(tos3[1], slots2)
      room:removePlayerMark(tos3[1], "@@laoshu-tos")
      room:addPlayerMark(tos3[1], "hx__sihai_can3",1)
    end
    ---处理蟑螂
    local tos4 = table.filter(room:getAlivePlayers(), function(p) 
      return p:getMark("@@zhanglang-tos") > 0
    end)
    if #tos4 > 0 then
      room:removePlayerMark(tos4[1], "@@zhanglang-tos")
      if tos4[1]:getMark("@@hx__sihai_target") > 0 then
        room:setPlayerMark(tos4[1], "@@hx__sihai_target", 0)
      end
    end
  end,
})

hx__sihai:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    if player:getMark("@@zhanglang-tos") == 0 then return false end
    local subcards = Card:getIdList(card)
    return #subcards > 0 and table.every(subcards, function(id)
      return table.contains(player:getCardIds(Player.Hand), id)
    end)
  end,
  prohibit_response = function(self, player, card)
    if player:getMark("@@zhanglang-tos") == 0 then return false end
    local subcards = Card:getIdList(card)
    return #subcards > 0 and table.every(subcards, function(id)
      return table.contains(player:getCardIds(Player.Hand), id)
    end)
  end,
})

hx__sihai:addEffect(fk.BeforeTurnOver,{
  anim_type = "control",
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__sihai.name) and target:getMark("@@wenzi-tos") > 0 and not target.faceup
  end,
  on_use = function(self, event, target, player, data)
    player:broadcastSkillInvoke(hx__sihai.name)
    player.room:doAnimate("InvokeSkill", {
      name = "hx__sihai",
      player = player.id,
      skill_type = "control",
    })
    return true
  end,
})



return hx__sihai